import indexRouter from './index'


function loadNav() {
  //获得路由数据
  var tokenData = JSON.parse(sessionStorage.getItem('loginData'));

  if (!!!tokenData) {
    return false;
  }
  var nvaList = JSON.parse(sessionStorage.getItem('loginData')).menu.SAFETY;
  //处理动态路由
  var foo = (data) => {
    var nav = [];
    for (var i = 0; i < data.length; i++) {
      if (data[i].children.length > 0) {
        nav.push({
          path: !!data[i].menuUrl ? '/' + data[i].menuUrl : '',
          name: !!data[i].menuUrl ? data[i].menuUrl : data[i].id,
          meta: {
            title: data[i].name,
            keepAlive: false,
            icon: data[i].menuIcon,
            hideInBread: data[i].menuHide
          },
          component: (!!data[i].menuComponent && data[i].menuComponent.length > 0) ? wzImport(data[i].menuComponent) : wzImport('main/test'),
          children: foo(data[i].children)
        })

      } else {
        nav.push({
          path: !!data[i].menuUrl ? '/' + data[i].menuUrl : '',
          name: !!data[i].menuUrl ? data[i].menuUrl : data[i].id,
          meta: {
            title: data[i].name,
            keepAlive: false,
            icon: data[i].menuIcon,
          },
          component: wzImport(data[i].menuComponent),
        })
      }
    }
    return nav;
  };
  const wzImport = file => () => import('@/' + file + '.vue');

  //获得处理后的动态路由
  var dataList = foo(nvaList);

  //动态路由处理
  indexRouter.addRoutes(dataList)

  var stateRoute = [
    {path: '*', redirect: "/404", meta: {hideInMenu: true}},
    {
      path: '/401', name: 'error_401', meta: {hideInMenu: true},
      component: () => import('@/components/error-page/401.vue')
    },
    {
      path: '/500', name: 'error_500', meta: {hideInMenu: true},
      component: () => import('@/components/error-page/500.vue')
    },
    {
      path: '/404', name: 'error_404', meta: {hideInMenu: true},
      component: () => import('@/components/error-page/404.vue')
    },

  ]
  //固定其它路由 追加 为了防止 刷新页面的时候 没有路由 直接跳转404  所以 统一追加
  indexRouter.addRoutes(stateRoute)

}

export default {loadNav}


export const navList = [
  {
    path: '/', name: "_login",
    meta: {title: '登录', keepAlive: false, hideInMenu: true},
    component: () => import('@/components/main/login.vue')
  },
  {
    path: '/mid', name: 'mid', meta: {hideInMenu: true},
    component: () => import('@/components/main/mid.vue')
  }
]

